<?php
Globals::requireController('Admin');
Globals::requireTable('Product');
Globals::requireTable('Days');
Globals::requireTable('Account');
Globals::requireTable('Order');

class AccountController extends AdminController
{
    protected $productTable;
    protected $daysTable;
    protected $accountTable;
    protected $orderTable;
    
	public function __construct($config = null)
	{
		parent::__construct($config, true);
		$this->productTable = new ProductTable($config);
		$this->daysTable = new DaysTable($config);
		$this->accountTable = new AccountTable($config);
		$this->orderTable = new OrderTable($config);
	}
	
	public function indexAction()
	{
	    $where['status'] = 1;
	    $count = $this->productTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $this->view->data = $this->productTable->listPage($where, 'id DESC', $pageSize, $pageId);
	}
	
	public function addAction()
	{
	    $id = $this->getIntParam('id');
	    if($id > 0){
	        $product = $this->productTable->getRow($id);
	        if($product['id'] > 0 && $product['status'] == 1){
	            $this->view->product = $product;
	        }
	    }
	}
	
	public function doaddAction()
	{
	    $id = $this->getIntParam('id');
	    $data['name'] = $this->getParam('name1');
	    $data['name_1'] = $this->getParam('name2');
	    $data['name_2'] = $this->getParam('name3');
	    $data['img'] = $this->getParam('img');
	    $data['img_1'] = $this->getParam('img2');
	    $data['img_2'] = $this->getParam('img3');
	    $data['rank'] = $this->getParam('rank');
	    $data['createdate'] = date('Y-m-d H:i:s');
	    if($id > 0){
	        $this->productTable->update($data,$id);
	    }else{
	        $this->productTable->add($data);
	    }
	    $this->redirectToAction();
	    exit();
	}
	
	public function deleteAction()
	{
	    $id = $this->getIntParam('id');
	    $this->productTable->update(array('status'=>0),$id);
	    $this->redirectToAction();
	    exit();
	}
	
	public function daysAction()
	{
	    $where['status'] = 1;
	    $pid = $this->getIntParam('pid');
	    if($pid > 0){
	        $where['pid'] = $pid;
	    }
	    $count = $this->daysTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $this->view->data = $this->daysTable->listPage($where, 'id DESC', $pageSize, $pageId);
	    $this->view->product = $this->productTable->listAll(array('status'=>1));
	    $this->view->pid = $pid;
	}
	
	public function daysaddAction()
	{
	    $id = $this->getIntParam('id');
	    if($id > 0){
	        $days = $this->daysTable->getRow($id);
	        if($days['id'] > 0){
	            $this->view->days = $days;
	        }
	    }
	    $this->view->product = $this->productTable->listAll(array('status'=>1));
	}
	
	public function daysdoaddAction()
	{
	    $id = $this->getIntParam('id');
	    $data['name'] = $this->getParam('name1');
	    $data['name_1'] = $this->getParam('name2');
	    $data['name_2'] = $this->getParam('name3');
	    $data['img'] = $this->getParam('img');
	    $data['img_1'] = $this->getParam('img2');
	    $data['img_2'] = $this->getParam('img3');
	    $data['pid'] = $this->getIntParam('pid');
	    $data['days'] = $this->getParam('days');
	    $data['money'] = $this->getParam('money');
	    $data['money_store'] = $this->getParam('money_store');
	    //$data['start_date'] = $this->getParam('start_date');
	    //$data['end_date'] = $this->getParam('end_date');
	    $data['rank'] = $this->getParam('rank');
	    $data['createdate'] = date('Y-m-d H:i:s');
	    if($id > 0){
	        $this->daysTable->update($data,$id);
	    }else{
	        $this->daysTable->add($data);
	    }
	    $this->redirectToAction('days');
	    exit();
	}
	
	public function daysdeleteAction()
	{
	    $id = $this->getIntParam('id');
	    $this->daysTable->update(array('status'=>0),$id);
	    $this->redirectToAction('days');
	    exit();
	}
	
	public function accountAction()
	{
	    $where['status'] = 1;
	    $pid = $this->getIntParam('pid');
	    if($pid > 0){
	        $where['pid'] = $pid;
	    }
	    $count = $this->accountTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $this->view->data = $this->accountTable->listPage($where, 'id DESC', $pageSize, $pageId);
	    $this->view->product = $this->productTable->listAll(array('status'=>1));
	    $this->view->pid = $pid;
	}
	
    public function accountaddAction()
	{
	    $id = $this->getIntParam('id');
	    if($id > 0){
	        $account = $this->accountTable->getRow($id);
	        if($account['id'] > 0){
	            $this->view->account = $account;
	            $time = time();
	        }
	    }
	    $this->view->product = $this->productTable->listAll(array('status'=>1));
	}
	
	public function accountdoaddAction()
	{
	    $id = $this->getIntParam('id');
	    $data['name'] = $this->getParam('name');
	    $data['pid'] = $this->getIntParam('pid');
	    $data['username'] = $this->getParam('username');
	    $data['nums'] = $this->getParam('nums');
	    $data['createdate'] = date('Y-m-d H:i:s');
	    $data['status'] = 1;
	    $account = $this->accountTable->getRow($id);
	    if($account['id'] > 0){
	        $this->accountTable->update($data,$id);
	    }else{
	        $this->accountTable->add($data);
	    }
	    $this->redirectToAction('account');
	    exit();
	}
	
	public function accountdeleteAction()
	{
	    $id = $this->getIntParam('id');
	    $this->accountTable->update(array('status'=>0),$id);
	    $this->redirectToAction('account');
	    exit();
	}
	
	public function updatepasswordAction(){
	    $id = $this->getIntParam('id');
	    $account = $this->accountTable->getRow($id);
	    $this->view->account = $account;
	    if($account['id'] < 1){
	        $this->redirectToAction('account');
	        exit();
	    }
	}
	
	public function doupdatepasswordAction(){
	    $id = $this->getIntParam('id');
	    $data['password'] = $this->getParam('password');
	    $data['endtime'] = 0;
	    $this->accountTable->update($data,$id);
	    $this->redirectToAction('account');
	    exit();
	}
	
	
}
